<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bing Maps Example</title>
    <!-- 引入OpenLayers的CSS样式 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol/ol.css">
    <style>
        /* 设置地图容器的大小 */
        #map {
            width: 100%;
            height: 500px;
            border: 1px solid #cc1d1d;
        }
    </style>
</head>
<body>
    <!-- 地图容器 -->
    <div id="map"></div>
    <!-- 图层选择下拉框 -->
    <div>
        <label for="layer-select">Select Layer:</label>
        <select id="layer-select">
            <option value="RoadOnDemand">Road</option>
            <option value="Aerial">Aerial</option>
            <option value="AerialWithLabelsOnDemand">Aerial with Labels</option>
            <option value="CanvasDark">Dark Canvas</option>
            <option value="OrdnanceSurvey">Ordnance Survey</option>
        </select>
    </div>

    <!-- 引入OpenLayers的JavaScript库 -->
    <script src="https://cdn.jsdelivr.net/npm/ol/ol.js"></script>
    <script>
        const styles = [
            'RoadOnDemand',
            'Aerial',
            'AerialWithLabelsOnDemand',
            'CanvasDark',
            'OrdnanceSurvey',
        ];
        const layers = [];
        let i, ii;
        for (i = 0, ii = styles.length; i < ii; ++i) {
            layers.push(
                new ol.layer.Tile({
                    visible: false,
                    preload: Infinity,
                    source: new ol.source.BingMaps({
                        key: 'Your Bing Maps Key from https://www.bingmapsportal.com/ here',
                        imagerySet: styles[i],
                        
                    }),
                })
            );
        }
        const map = new ol.Map({
            layers: layers,
            target: 'map',
            view: new ol.View({
                center: [-6655.5402445057125, 6709968.258934638],
                zoom: 13,
            }),
        });

        const select = document.getElementById('layer-select');
        function onChange() {
            const style = select.value;
            for (let i = 0, ii = layers.length; i < ii; ++i) {
                layers[i].setVisible(styles[i] === style);
            }
        }
        select.addEventListener('change', onChange);
        onChange();
    </script>
</body>
</html>
